<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>orderPreview</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
    <!--    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"-->
    <!--          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>-->
    <!--    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>-->
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"/>
    <script th:src="@{/js/vue.js}"></script>
</head>
<body>

<div th:insert="publicHeader"/>
<script th:src="@{/js/publicHeader.js}"></script>


<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">


    <div class="container">
        <div class="row">
            <div class="col-md-8">
                <select class="form-control" name="address" v-model="selectAddressId">
                    <option v-for="a in addressList" :value="a.id">{{a.address}}  {{a.name}}  {{a.tel}}</option>
                </select>
            </div>
            <div class="col-md-4">
                <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" @click="clearAddAddress()">
                    新增地址
                </button>
            </div>
        </div>
    </div>
    <br/>

    <div class="row">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <table class="table">

                        <tr>
                            <th>商品名称</th>
                            <th>商品图片</th>
                            <th>商品单价(元)</th>
                            <th>购买数量</th>
                            <th>小计</th>
                        </tr>
                        <tr v-for="cartItemVo in carItems">
                            <td v-text="cartItemVo.productname" style="height:60px;line-height: 60px">商品名称</td>
                            <td><img :src="'/'+cartItemVo.productimgsrc"
                                     style="width: 60px;height: 60px; border-radius: 50%"/></td>
                            <td v-text="cartItemVo.productprice" style="height:60px;line-height: 60px">商品单价(元)</td>
                            <td v-text="cartItemVo.buycount" style="height:60px;line-height: 60px">购买数量</td>
                            <td v-text="cartItemVo.sumprice" style="height:60px;line-height: 60px">小计</td>
                        </tr>

                    </table>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <b> 总价(元):{{totalPrice}} </b>
                </div>

                <div class="col-md-6">
                    <button class="btn btn-success" @click="submitOrder">提交订单</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         data-backdrop="static" data-keyboard="false">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新增地址</h4>
                </div>
                <div class="modal-body">

                    <form>

                        <div class="form-group">
                            <label for="address">详细地址</label>
                            <input type="text" class="form-control" id="address" v-model="addressFrom.address"
                                   placeholder="请输入详细地址">
                        </div>

                        <div class="form-group">
                            <label for="tel">手机号</label>
                            <input type="text" class="form-control" id="tel" v-model="addressFrom.tel"
                                   placeholder="请输入手机号">
                        </div>

                        <div class="form-group">
                            <label for="name">收件人姓名</label>
                            <input type="text" class="form-control" v-model="addressFrom.name" id="name"
                                   placeholder="请输入收件人">
                        </div>

                    </form>


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" @click="addAddress">保存</button>
                </div>
            </div>
        </div>
    </div>


</div>

<script>

    console.log(".........orderPreview...........")

    new Vue({
        el: "#app"
        ,
        data: {
            productIds: [[${productIds}]],
            carItems: [],
            totalPrice: "0.00",
            addressFrom: {
                address: "",
                tel: "",
                name: ""
            },
            addressList: [],
            selectAddressId:-1
        }
        ,
        methods: {
            initOrderPreview() {
                var vueObj = this;
                $.post("/order/orderCartItem", {productIds: vueObj.productIds}, function (data) {
                    vueObj.carItems = data.cartItemVoList;
                    vueObj.totalPrice = data.totalPrice;
                })
            },
            clearAddAddress() {
                //每次点击新增地址钱，清除原有记录
                this.addressFrom = {
                    address: "",
                    tel: "",
                    name: ""
                }
            },
            addAddress() {
                console.log(this.addressFrom)
                var vueObject = this;
                $.post("/address/add", this.addressFrom, function (data) {
                    if (data = "ok") {
                        //增加完成后重新获取所有的地址
                        vueObject.initAddressList()
                        //保存地址，添加完成后关闭新增地址小窗口
                        $("#myModal").modal("hide")
                    } else {
                        //弹出错误信息
                        alert(data)
                    }
                })
            },
            initAddressList() {
                var vueObject = this;
                $.post("/address/getAddressList", this.addressFrom, function (data) {
                    vueObject.addressList=data
                })
            },
            submitOrder(){
                if (this.selectAddressId==-1){
                    alert("请确定一个收货地址")
                    return;
                }
                var param={
                    productIds:this.productIds,
                    selectAddressId:this.selectAddressId
                }

                $.post("/order/addOrder",param,function (data) {
                if (data="ok"){
                    location.href="/myOrder.html";
                }else {
                    alert(data)
                }
                })
            }
        }
        ,
        created() {
            this.initOrderPreview()
            this.initAddressList()
        }

    });


</script>


<div th:insert="publicFooter"/>
<script th:src="@{/js/publicFooter.js}"></script>


</body>
</html>